package jp.co.ricoh.melody.views.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import jp.co.ricoh.melody.R;

/**
 * Created by tanyajie on 2016/11/16.
 */

public class SoundTimeView extends BaseAdapter {
    private List<String> mList;
    private Context context;
    private int mSelectedItemPosition;


    public SoundTimeView(List<String> mList, Context context, String time) {
        this.mList = mList;
        this.context = context;
        for(int i = 0; i < mList.size(); i++) {
            if(mList.get(i).equals(time)) {
                mSelectedItemPosition = i;
                break;
            }
        }
    }

    public int getSelectedItemPosition() {
        return mSelectedItemPosition;
    }



    @Override
    public int getCount() {
            return mList.size();
        }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null) {
            convertView = View.inflate(context, R.layout.sound_time_item_list, null);
            viewHolder = new ViewHolder();
            viewHolder.info = (TextView) convertView.findViewById(R.id.sound_time_item_list_txt);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.info.setText(mList.get(position));
        viewHolder.info.setBackgroundColor(Color.WHITE);

        viewHolder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelectedItemPosition = position;
                notifyDataSetChanged();
            }
        });
        if(mSelectedItemPosition == position){
            viewHolder.info.setBackgroundColor(Color.BLUE);
        }
        return convertView;
    }

    public final class ViewHolder{
        public TextView info;
    }


}
